package com.b511.swpu.cementbackend.service.impl;


import com.b511.swpu.cementbackend.dto.output.BasicOutputDto;
import com.b511.swpu.cementbackend.entity.CM_WK_BASIC;

import com.b511.swpu.cementbackend.repository.CM_WorkBasicRepository;

import com.b511.swpu.cementbackend.service.CmWkBasicService;
import com.b511.swpu.cementbackend.util.R;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CmWkBasicServiceImpl implements CmWkBasicService {


    private final CM_WorkBasicRepository cm_workBasicRepository;

    public CmWkBasicServiceImpl(CM_WorkBasicRepository cm_workBasicRepository) {

        this.cm_workBasicRepository = cm_workBasicRepository;
    }


    @Override
    public R getCmWkBasicSomeNews(Integer pageNum,Integer pageSize,String wellName,String caseSize) {
        List<CM_WK_BASIC> lists = null;
        if(pageNum==null&pageNum.equals("")){
            pageNum = 0;
        }else {
            pageNum = pageNum-1;
        }
        Page<CM_WK_BASIC> page =null;
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        if (wellName.length()<=0 & caseSize.length()<=0) {
            page = cm_workBasicRepository.getCmWkBasicSomeNews(pageable);
            lists = page.getContent();
        }else if(wellName.length()>0&caseSize.length()<=0){
            page =  cm_workBasicRepository.getCmWkBasicSomeNewsByWellName(wellName,pageable);
            lists = page.getContent();
        }else if(wellName.length()<=0&caseSize.length()>0){
            page = cm_workBasicRepository.getCmWkBasicSomeNewsByCaseSize(caseSize,pageable);
            lists = page.getContent();
        }else {
            page = cm_workBasicRepository.getCmWkBasicSomeNews(wellName, caseSize,pageable);
            lists = page.getContent();
        }

        BasicOutputDto outputDto = new BasicOutputDto();
        outputDto.setTotalNumber(page.getTotalElements());
        outputDto.setBasicList(lists);

        return R.ok().putData(outputDto);
    }

    @Override
    public R getCmWkBasicByuploadPerson(String uploadPerson, Integer pageNum, Integer pageSize, String wellName, String caseSize) {
        List<CM_WK_BASIC> lists = null;
        if(pageNum==null&pageNum.equals("")){
            pageNum = 0;
        }else {
            pageNum = pageNum-1;
        }
        Page<CM_WK_BASIC> page =null;
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        if (wellName.length()<=0 & caseSize.length()<=0) {
            page = cm_workBasicRepository.getCmWkBasicByuploadPerson(uploadPerson,pageable);
            lists = page.getContent();
        }else if(wellName.length()>0&caseSize.length()<=0){
            page =  cm_workBasicRepository.getCmWkBasicByuploadPersonAndWellName(uploadPerson,wellName,pageable);
            lists = page.getContent();
        }else if(wellName.length()<=0&caseSize.length()>0){
            page = cm_workBasicRepository.getCmWkBasicByuploadPersonAndCaseSize(uploadPerson,caseSize,pageable);
            lists = page.getContent();
        }else {
            page = cm_workBasicRepository.getCmWkBasicByuploadPerson(uploadPerson,wellName, caseSize,pageable);
            lists = page.getContent();
        }

        BasicOutputDto outputDto = new BasicOutputDto();
        outputDto.setTotalNumber(page.getTotalElements());
        outputDto.setBasicList(lists);

        return R.ok().putData(outputDto);
    }

    @Override
    public R getCmWkBasicByCheckPerson(String checkPerson, Integer pageNum, Integer pageSize, String wellName, String caseSize) {
        List<CM_WK_BASIC> lists = null;
        if(pageNum==null&pageNum.equals("")){
            pageNum = 0;
        }else {
            pageNum = pageNum-1;
        }
        Page<CM_WK_BASIC> page =null;
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        if (wellName.length()<=0 & caseSize.length()<=0) {
            page = cm_workBasicRepository.getCmWkBasicByCheckPerson(checkPerson,pageable);
            lists = page.getContent();
        }else if(wellName.length()>0&caseSize.length()<=0){
            page =  cm_workBasicRepository.getCmWkBasicByCheckPersonAndWellName(checkPerson,wellName,pageable);
            lists = page.getContent();
        }else if(wellName.length()<=0&caseSize.length()>0){
            page = cm_workBasicRepository.getCmWkBasicByCheckPersonAndCaseSize(checkPerson,caseSize,pageable);
            lists = page.getContent();
        }else {
            page = cm_workBasicRepository.getCmWkBasicByCheckPerson(checkPerson,wellName, caseSize,pageable);
            lists = page.getContent();
        }

        BasicOutputDto outputDto = new BasicOutputDto();
        outputDto.setTotalNumber(page.getTotalElements());
        outputDto.setBasicList(lists);

        return R.ok().putData(outputDto);
    }

    @Override
    @Transactional
    public  R   PassValidate(String wellName,String caseSize, String checkPerson){
        int row=  cm_workBasicRepository.updateCM_WK_BASICByWellNameAndCaseSize(wellName,caseSize, checkPerson);
        return R.ok();
    }

}
